package com.example.chatapp.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.chatapp.R;
import com.example.chatapp.bean.EntityInfo;
import com.example.chatapp.util.ChatUtil;

import java.util.List;

public class EntityListAdapter extends BaseAdapter {
    private Context mContext;
    private List<EntityInfo> mUserList;

    public EntityListAdapter(Context context, List<EntityInfo> user_list) {
        this.mContext = context;
        this.mUserList = user_list;
    }

    @Override
    public int getCount() {
        return mUserList.size();
    }

    @Override
    public Object getItem(int i) {
        return mUserList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user, null);
            holder.iv_portrait = convertView.findViewById(R.id.iv_portrait);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_relation = convertView.findViewById(R.id.tv_relation);
            convertView.setTag(holder); //将试图持有者保存到转换视图中
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EntityInfo user = mUserList.get(position);
        holder.tv_name.setText(user.name);
        holder.tv_relation.setText(user.relation);
        Drawable drawable = ChatUtil.getPortraitByName(mContext, user.name); //获取用户头像
        holder.iv_portrait.setImageDrawable(drawable); //显示用户头像
        return convertView;
    }

    public final class ViewHolder {
        public ImageView iv_portrait; // 声明用户头像的图像视图对象
        public TextView tv_name; // 声明用户名称的文本视图对象
        public TextView tv_relation; // 声明用户关系的文本视图对象
    }
}
